package com.itheima.consumer.controller;

import com.itheima.consumer.feign.UserFeignClient;
import com.itheima.consumer.pojo.ConsumerInfo;
import com.itheima.consumer.pojo.MessageInfo;
import com.itheima.consumer.pojo.UserInfo;
import com.itheima.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RefreshScope
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private UserFeignClient userFeignClient;

    @Value("${chat.off}")
    private Integer status;
    /**
     * 远程调用获取 暂时写死
     * @return
     */
    @GetMapping("/getUserInfo")
    public UserInfo getUserInfo(){
        return userFeignClient.getUserInfo();
//        return UserInfo.builder().userName("user").head("https://img2.baidu.com/it/u=1295739140,2463989310&fm=26&fmt=auto&gp=0.jpg").build();
    }

    /**
     * 写死
     * @return
     */
    @GetMapping("/getConsumerInfo")
    public ConsumerInfo getConsumerInfo(){
        return ConsumerInfo.builder().consumerName("consumer").head("https://img1.baidu.com/it/u=514478265,585302626&fm=26&fmt=auto&gp=0.jpg").build();
    }
    /**
     * 获取历史
     * @return
     */
    @GetMapping("/getMessage")
    public List<MessageInfo> getMessage(){
        return consumerService.getHistoryMessage();
    }

    @PostMapping("/sendMessage")
    public String sendMessage(@RequestBody MessageInfo messageInfo){
        if(status==1){
            try {
                consumerService.sendMessage(messageInfo);
                return "yes";
            } catch (Exception e) {
                e.printStackTrace();
                return "no";
            }
        }else {
            return "down";
        }
    }

}
